import { _decorator, Button, find, instantiate, Label, NodePool, tween, Tween, UITransform } from 'cc';
import { BasePopUp } from '../../../frame/component/ui.pop-up';
import GameDC from '../../data/data.game.center';
import DataManager from '../../manager/data.manager';
import { Float } from '../page/float';
import type { Popup } from '../page/pop-up';
const { ccclass, property } = _decorator;

@ccclass('ppDiamond')
export class ppDiamond extends BasePopUp<typeof Popup, null> {
	protected default_return: null;

	private ui_container: UITransform;

	private goods_info_map: Map<string, typeof DataManager.diamond_goods[number]>;

	private goods_inited: mtec.NudityPromise<'end'>;

	protected initAfterOnLoad(){
		this.init();
	}

	public onShow(){
		GameDC.Trigger.HILIT_DIAMOND = true;
		BasePopUp.jelly_enter(this.ui_container.node);

		this.goods_inited.promise.then(()=>{
			this.ui_container.node.children.forEach(node=>{
				let light = node.getChildByName('background-light');
				Tween.stopAllByTarget(light);
				tween(light).set({angle: 0})
				.to(5, {angle: -360}).union()
				.repeatForever().start();
			});
		});
	}

	private init(){
		this.ui_container = this.node.getChildByName('container').getComponent(UITransform);
		this.goods_info_map = new Map();
		this.goods_inited = new mtec.NudityPromise();
		this.initGoodsList();
	}

	private btn_lock: boolean = false;
	protected _click_event_: { [name: string]: (this: ppDiamond, button: Button) => void; } = {
		close(btn){
			if(this.btn_lock){
				return void 0;
			}
			this.btn_lock = true;

			this.exit();
		},
		reap(btn){
			if(this.btn_lock){
				return void 0;
			}
			this.btn_lock = true;

			this.reap(btn);
		}
	}

	private async initGoodsList(){
		let pool = new NodePool();
		let demo = this.ui_container.node.children[0];

		[...this.ui_container.node.children].forEach(n=>pool.put(n));

		await DataManager.ConfigInited;

		DataManager.diamond_goods.forEach(info=>{
			let item = pool.get() || instantiate(demo);
			if(info.Identifier){
				item.name = info.Identifier;
			}else{
				item.name = [info.diamond, info.price].join('&');
			}
			this.goods_info_map.set(item.name, info);
			item.setParent(this.ui_container.node);

			find('diamond/lbl-value', item).getComponent(Label).string = info.diamond.toString();

			['mark-freshman', 'lbl-freshman'].forEach(name=>item.getChildByName(name).active = info.isFreshman);

			if(info.isFreshman){
				find('lbl-freshman', item).getComponent(Label).string = 'Newcomer\nDiscount';
			}

			['background-discount', 'lbl-DISCOUNT', 'lbl-discount'].forEach(name=>item.getChildByName(name).active = Boolean(info.discount));

			item.getChildByName('lbl-DISCOUNT').getComponent(Label).string = 'Discount';
			item.getChildByName('lbl-discount').getComponent(Label).string = info.discount+'%';

			item.getChildByName('icon-goods').children.forEach(n=>n.active = n.name == info.icon);

			let isGoods = Boolean(info.Identifier);

			let btn_reap = item.getChildByName('reap').getComponent(Button);
			let [lbl_light, lbl_dark] = ['lbl-light', 'lbl-dark'].map(name=>item.getChildByName(name).getComponent(Label));
			let [bg_light, bg_dark] = ['bg-light', 'bg-dark'].map(name=>btn_reap.node.getChildByName(name));

			if(isGoods){
				btn_reap.interactable = true;
				bg_light.active = true;
				bg_dark.active = false;
				lbl_light.node.active = true;
				lbl_dark.node.active = false;
				lbl_light.string = info.localPrice;
			}else{
				let itabel = !GameDC.User.free_diamond_reap;
				btn_reap.interactable = itabel;
				bg_light.active = itabel;
				bg_dark.active = !itabel;
				lbl_light.node.active = itabel;
				lbl_dark.node.active = !itabel;

				lbl_light.string = 'Getting';
				lbl_dark.string = 'Already received';
			}
		});

		this.goods_inited.resolve('end');
	}

	private exit(){
		BasePopUp.jelly_leave(this.ui_container.node)
		.then(()=>{
			this.close(null);
			this.btn_lock = false;
			this.ui_container.node.children.forEach(node=>{
				let light = node.getChildByName('background-light');
				Tween.stopAllByTarget(light);
				light.angle = 0;
			});
			GameDC.Trigger.HILIT_DIAMOND = false;
		});
	}

	private async reap(btn: Button){
		let info = this.goods_info_map.get(btn.node.parent.name);

		let isGoods = Boolean(info.Identifier);

		if(isGoods){
			let p = DataManager.buyDiamond(info.goodsId);
			await this.pop.Wait(p);

			switch((await p).status){
				case 'trade-success':
					DataManager.floatDiamond(info.diamond, btn.node.parent.getChildByName('icon-goods').worldPosition);
					Float.Hint('Purchase successful');
					this.exit();
					break;
				case 'trade-cancel':
					Float.Hint('Cancelled');
					this.btn_lock = false;
					break;
				case 'trade-fail':
					Float.Hint('Purchase failed');
					this.btn_lock = false;
					break;
			}
		}else if(!GameDC.User.free_diamond_reap){
			GameDC.User.free_diamond_reap = true;
			btn.node.parent.getChildByName('lbl-light').active = false;
			btn.node.parent.getChildByName('lbl-dark').active = true;
			btn.node.getChildByName('bg-light').active = false;
			btn.node.getChildByName('bg-dark').active = true;
			DataManager.changeDiamond(info.diamond);
			DataManager.floatDiamond(info.diamond, btn.node.parent.getChildByName('icon-goods').worldPosition);
			this.btn_lock = false;
		}else{
			this.btn_lock = false;
		}
	}
}

